# 项目:
# 文件: demo1
# 作者: Yang
# 日期: 2025/5/20 14:04
# coding:utf-8
import random
from platform import uname
from time import sleep

from Scripts.runxlrd import options
from selenium import webdriver
import time

from selenium.webdriver.support.select import Select


def get_driver():
    driver = webdriver.Edge()
    driver.implicitly_wait(3)
    driver.maximize_window()
    return driver

def write(element,value):
    element.click()
    element.clear()
    element.send_keys(value)

def select_random_first(element):
    options_count = len(Select(element).options)
    random_index = random.randint(1,options_count - 1)
    Select(element).select_by_index(random_index)

def test_single_choice_question():        # 测试单选题
    # 获取登录页面元素，并登录
    driver = get_driver()
    driver.get('http://localhost:8080/YunDuanBoss/login/logout')
    uname = driver.find_element('name', 'userName')
    write(uname, 'YDXA519')
    upass = driver.find_element('name', 'userPass')
    write(upass, 'YunDuan123')
    driver.find_element('css selector', '#form-login > div > div > div.modal-footer > button').click()
    sleep(3)
    # 进入题库管理页面并点击单选题
    driver.get('http://localhost:8080/YunDuanBoss/exercise')
    driver.find_element('css selector',
                        '#content > div.row.con-margin.con-body-con > div.col-lg-12.col-md-12.col-xs-12.con-body-padding.search-blank.text-left > div > button:nth-child(1)').click()
    sleep(2)
    # 随机选择方向名称
    scb_way = driver.find_element('css selector',
                                     '#addOption-form > div > div > div:nth-child(3) > div.col-md-6.col-sm-6.form-group > select')
    select_random_first(scb_way)
    # 随机选择阶段名
    scb_stage = driver.find_element('css selector',
                                       '#addOption-form > div > div > div:nth-child(3) > div:nth-child(2) > select')
    select_random_first(scb_stage)
    # 随机选择课程名
    scb_class = driver.find_element('css selector', '#addOption-form > div > div > div:nth-child(4) > div > select')
    select_random_first(scb_class)
    # 输入合法的题干
    topic = driver.find_element('id', 'econtent1')
    write(topic, '1+1=?')
    # 输入合法的答案
    optionA = driver.find_element('css selector', '#ul > div:nth-child(2) > input.text')
    write(optionA, '1')
    optionB = driver.find_element('css selector', '#ul > div:nth-child(3) > input.text')
    write(optionB, '2')
    optionC = driver.find_element('css selector', '#ul > div:nth-child(4) > input.text')
    write(optionC, '3')
    optionD = driver.find_element('css selector', '#ul > div:nth-child(5) > input.text')
    write(optionD, '4')
    # 添加选项
    add_option = driver.find_element('css selector',
                                     '#ul > div:nth-child(1) > span.glyphicon.glyphicon-plus-sign').click()
    sleep(2)
    # 删除选项
    delete_option = driver.find_element('css selector',
                                        '#ul > div:nth-child(1) > span.glyphicon.glyphicon-minus-sign').click()
    sleep(2)
    # 录入正确答案
    answer = driver.find_element('css selector', '#ul > div:nth-child(3) > input[type=radio]:nth-child(3)').click()
    # 保存
    scb_save = driver.find_element('id', 'addOptionBtn').click()
    sleep(3)

def test_completion_question():     # 测试填空题
    # 获取登录页面元素，并登录
    driver = get_driver()
    driver.get('http://localhost:8080/YunDuanBoss/login/logout')
    uname = driver.find_element('name', 'userName')
    write(uname, 'YDXA519')
    upass = driver.find_element('name', 'userPass')
    write(upass, 'YunDuan123')
    driver.find_element('css selector', '#form-login > div > div > div.modal-footer > button').click()
    sleep(3)

    # 进入题库管理页面并点击填空题
    driver.get('http://localhost:8080/YunDuanBoss/exercise')
    driver.find_element('css selector','#content > div.row.con-margin.con-body-con > div.col-lg-12.col-md-12.col-xs-12.con-body-padding.search-blank.text-left > div > button:nth-child(2)').click()

    # 随机选择方向名称
    gfb_way = driver.find_element('css selector','#addBlank-form > div > div > div:nth-child(1) > div.col-md-6.col-sm-6.form-group > select')
    select_random_first(gfb_way)
    # 随机选择阶段名
    gfb_stage = driver.find_element('css selector',
                                       '#addBlank-form > div > div > div:nth-child(1) > div:nth-child(3) > select')
    select_random_first(gfb_stage)
    # 随机选择课程名
    gfb_class = driver.find_element('css selector', '#addBlank-form > div > div > div:nth-child(2) > div > select')
    select_random_first(gfb_class)

    # 输入合法内容
    gfb_content = driver.find_element('css selector','#econtent2')
    write(gfb_content, '2+2=')
    add_content = driver.find_element('css selector','#addBlank-form > div > div > div:nth-child(3) > div > label').click()

    # 输入合法的答案
    gfb_answer = driver.find_element('css selector','#addBlank-form > div > div > div.row.blank-answer-box > div > input')
    write(gfb_answer,'4')

    # 保存数据
    gfb_save = driver.find_element('css selector','#addBlank > div > div > div.modal-footer > button').click()

    # 刷新页面，返回题库管理页面
    driver.refresh()


if __name__ == '__main__':

    test_single_choice_question()
    test_completion_question()